﻿using aier.business.logic;
using aier.dataaccess.model;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 筛查客户
    /// </summary>
    //[Authorize]
    [RoutePrefix("api/ScreenCustomer")]
    public class ScreenCustomerController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILog log { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public CommonBll commonBll { get; set; }
        /// <summary>
        /// 逻辑
        /// </summary>
        public ScreenCustomerBll screenCustomerBll { get; set; }

        /// <summary>
        /// 根据筛查ID,客户ID查询筛查客户对像视图
        /// 原程序 ScreeningServiceImpl-> selectScCustomer(String scid, String customerid)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="customerId">潜客ID</param>c 
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenCustomerByScreenIdAndCustomerId(Guid scid, Guid customerId)
        {
            try
            {
                var screen = screenCustomerBll.GetScreenCustomerByScreenIdAndCustomerId(scid,  customerId);
                return Ok<ScreenCustomerView>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查ID查询进行中的筛查客户对像视图
        /// 原程序 ScreeningServiceImpl-> selectScCustomerNotCompleteByScid(String scid)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenCustomerNotCompleteByScreenId(Guid scid)
        {
            try
            {
                var screen = screenCustomerBll.GetScreenCustomerNotCompleteByScreenId(scid);
                return Ok<IEnumerable<ScreenCustomerView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查ID查询所有筛查客户对像视图
        /// 原程序 ScreeningServiceImpl-> selectScCustomerAllState(String scid)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAllScreenCustomerByScreenId(Guid scid)
        {
            try
            {
                var screen = screenCustomerBll.GetAllScreenCustomerByScreenId(scid);
                return Ok<IEnumerable<ScreenCustomerView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查ID和状态查询筛查客户列表
        /// 原程序 ScreeningServiceImpl-> selectScCustomerByScidAndState(String scid, String scstate)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="scstate">筛查ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenCustomerByScreenIdAndState(Guid scid, int scstate)
        {
            try
            {
                var screen = screenCustomerBll.GetScreenCustomerByScreenIdAndState(scid, scstate);
                return Ok<IEnumerable<ScreenCustomerView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查ID和客户筛查状态为进行中,(姓名,电话,身份证,学号模糊查询)查询筛查客户列表
        /// 原程序 ScreeningServiceImpl-> selectScCustomerByScidAndKey(String scid, String key) 
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="key">筛查ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenCustomerByScreenIdAndKey(Guid scid, string key)
        {
            try
            {
                var screen = screenCustomerBll.GetScreenCustomerByScreenIdAndKey(scid, key);
                return Ok<IEnumerable<ScreenCustomerView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查ID和客户筛查状态,(姓名,电话,身份证,学号模糊查询)查询筛查客户列表
        /// 原程序 ScreeningServiceImpl-> selectByScidStateAndKey(String scid, Integer scstate, String key, int fromIndex, int toIndex) 
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="scstate">筛查客户状态 0：筛查未完成 1：筛查已完成 2：未预约 3：已预约 4：全部</param>
        /// <param name="key">姓名,电话,身份证,学号</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenCustomerByScreenIdAndSCStaceAndKeyPaging(Guid scid, int scstate, string key, int pageIndex, int pageSize)
        {
            try
            {
                var screen = screenCustomerBll.GetScreenCustomerByScreenIdAndSCStaceAndKeyPaging(scid, scstate, key, pageIndex, pageSize);
                return Ok<PageResult<ScreenCustomerView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查客户ID查询筛查客户
        /// 原程序 ScreeningServiceImpl-> selectScCustomer(String customerId)
        /// </summary>
        /// <param name="customerid">筛查客户ID</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenCustomerByCustomerId(Guid customerid)
        {
            try
            {
                var screen = screenCustomerBll.GetScreenCustomerByCustomerId(customerid);
                return Ok<IEnumerable<ScreenCustomerView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查编号,客户名称查询筛查报告
        /// 原程序 ScreeningServiceImpl-> selectScCustomerByscNum(String scNum, String scName)
        /// </summary>
        /// <param name="scnum">筛查编号</param>
        /// <param name="fullname">客户名称</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenCustomerBySCNum(string scnum, string fullname)
        {
            try
            {
                var screen = screenCustomerBll.GetScreenCustomerBySCNum(scnum, fullname);
                return Ok<IEnumerable<ScreenCustomerView>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 根据筛查编号,客户名称查询重名客户数量
        /// 原程序 ScreeningServiceImpl-> countCustomernameByScid(String scid, String customername)
        /// </summary>
        /// <param name="scid">筛查ID</param>
        /// <param name="fullname">客户名称</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetCountScreenCustomerBySCNumAndFullName(string scid, string fullname)
        {
            try
            {
                var screen = screenCustomerBll.GetCountScreenCustomerByScidAndFullName(scid, fullname);
                return Ok<int>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 查询筛查完成的客户id
        /// 原程序 ScreeningServiceImpl-> selectFinshedCustomerId(String school, String classnum) 
        /// </summary>
        /// <param name="school">学校ID</param>
        /// <param name="classnum">班级号</param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetScreenCustomerBySchoolAndClassNum(Guid school, string classnum)
        {
            try
            {
                var screen = screenCustomerBll.GetScreenCustomerBySchoolAndClassNum(school, classnum);
                return Ok<IEnumerable<dynamic>>(screen);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 保存筛查客户
        /// 如果leadid为空表示需要新增客户,否则为更新客户
        /// 原程序 ： public Result saveScCustomer(DoctorUser user, TLeadBase lead, String scid, String teamid)
        /// </summary>
        /// <param name="lead">潜客对像</param>
        /// <param name="scid">筛查ID</param>
        /// <param name="teamid">筛查组ID</param>
        /// <returns></returns>
        public IHttpActionResult SaveScreenCustomer(LeadInfo lead, Guid scid, Guid teamid)
        {
            //try
            //{
            //    var screen = commonBll.SaveScreenCustomer(lead, scid, teamid);
            //    return Ok<ResResult>(screen);
            //}
            //catch (Exception ex)
            //{
            //    log.Error(ex.Message, ex);
                return new ApiErrorResult("", Request);
            //}
        }

        /// <summary>
        /// 保存筛查客户
        /// 如果leadid为空表示需要新增客户,否则为更新客户
        /// 原程序 ： saveScCustomer4School(DoctorUser user, String scid, String teamid, String customerid, String ownerid) 
        /// </summary>
        /// <param name="customer">潜客客户对像</param>
        /// <returns></returns>
        public IHttpActionResult Post(ScreenCustomer customer)
        {
            try
            {
                if(customer.new_customerid!=Guid.Empty && customer.new_scid !=Guid.Empty)
                {
                    var screen = screenCustomerBll.CreateCustomer(customer);
                    return Ok<ResResult>(screen);
                }

                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }

        /// <summary>
        /// 更新筛查客户
        /// 如果leadid为空表示需要新增客户,否则为更新客户
        /// </summary>
        /// <param name="customer">潜客客户对像</param>
        /// <returns></returns>
        public IHttpActionResult Put(ScreenCustomer customer)
        {
            try
            {
                if (customer.new_customerid != Guid.Empty && customer.new_scid != Guid.Empty && customer.new_sc_customerid != Guid.Empty)
                {
                    var screen = screenCustomerBll.CreateCustomer(customer);
                    return Ok<ResResult>(screen);
                }

                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }


        /// <summary>
        /// 根据潜客Id获取筛查客户信息
        /// </summary>
        /// <param name="leadId">leadId</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetScreenCustomerByLeadId")]
        public IHttpActionResult GetScreenCustomerByLeadId(Guid leadId)
        {
            try
            {
                if (leadId != Guid.Empty)
                {
                    var data = screenCustomerBll.GetScreenCustomerByLeadId(leadId);
                    return Ok(data);
                }
                return NotFound();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return new ApiErrorResult(ex.Message, Request);
            }
        }



    }
}
